#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define re register int
using namespace std;
inline int read(){
	int x=0,w=1;
	char ch=getchar();
	while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
	if(ch=='-') w=-1,ch=getchar();
	while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-48,ch=getchar();
	return x*w;
}
long long h[1000001],hh[1000001],c[1000001],r[1000001],l[1000001];
int main() {
    long long n,maxx,maxi,ans;
    scanf("%lld",&n);
    for(long long i=0;i<n;i++) scanf("%lld",&h[i]);
    maxi=0;
    maxx=h[0];
    for(long long i=1;i<n;i++) if(h[i]>maxx) maxx=h[i],maxi=i;
    for(long long i=0;i<n;i++) hh[i]=h[(maxi+i)%n];
    hh[n]=hh[0];
    c[n]=0;
    for(long long i=n-1;i>=0;i--) {
        r[i]=i+1;
        while(r[i]<n&&hh[i]>hh[r[i]])
            r[i]=r[r[i]];
        if(r[i]<n&&hh[i]==hh[r[i]]) {
            c[i]=c[r[i]]+1;
            r[i]=r[r[i]];
        }
    }
    l[0]=0;
    for(long long i=1;i<=n;i++) {
        l[i]=i-1;
        while(l[i]>0&&hh[i]>=hh[l[i]]) l[i]=l[l[i]];
    }
    ans=0;
    for(long long i=0;i<n;i++) {
        ans+=c[i];
        if(hh[i]<hh[0]) {
            if(l[i]==0&&r[i]==n) ans++;
            else ans+=2;
        }
    }
    printf("%lld\n",ans);
    return 0;
}
